<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-calendar"></i> 楼栋基本信管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>楼栋编辑</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="form-box">
        <el-form ref="formRef" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="所属校区" prop="region">
            <el-input
                v-model="form.region"
                placeholder="命名格式如:西校区"
            ></el-input>
          </el-form-item>
          <el-form-item label="公寓名称" prop="name">
            <el-input
                v-model="form.name"
                placeholder="命名格式如:一公寓"
            ></el-input>
          </el-form-item>
          <el-form-item label="公寓类型" prop="type">
            <!-- <el-input
              v-model="form.type"
              placeholder="命名格式如:男生公寓"
            ></el-input> -->
            <div>
              <el-radio v-model="form.type" label="男生公寓" border>男生公寓</el-radio>
              <el-radio v-model="form.type" label="女生公寓" border>女生公寓</el-radio>
            </div>
          </el-form-item>
          <el-form-item label="所在楼层" prop="floor">
            <el-input v-model="form.floor" type='number'></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">提交</el-button>
            <el-button @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import {reactive, ref} from "vue";
import {ElMessage} from "element-plus";
import request from "../../utils/request";
import {useRoute, useRouter} from "vue-router";
import {useStore} from "vuex";

export default {
  name: "AddBuilding",
  setup() {
    const root = "/building/";
    const router = useRouter();
    const route = useRoute();
    const store = useStore();
    const rules = {
      region: [{required: true, message: "请输入校区", trigger: "blur"}],
      name: [{required: true, message: "请输入楼栋名", trigger: "blur"}],
      type: [{required: true, message: "请输入类型", trigger: "blur"}],
      floor: [{required: true, message: "请输入楼层", trigger: "blur"}],
    };
    const formRef = ref(null);
    var form = reactive({
      sid: "",
      region: "",
      name: "",
      type: "",
      floor: "",
    });
    if (route.query.id) {
      request.get(root + route.query.id).then((res) => {
        form.sid = route.query.id;
        form.region = res.data.region;
        form.name = res.data.name;
        form.type = res.data.type;
        form.floor = res.data.floor;
      });
    }
    // 提交
    const onSubmit = () => {
      // 表单校验
      formRef.value.validate((valid) => {
        if (valid) {
          var method = form.sid === "" ? "POST" : "PUT";
          request({
            url: "/building",
            method: method,
            data: form,
          }).then((res) => {
            if (res.code == 18) {
              ElMessage.error(res.msg);
            } else {
              ElMessage.success("提交成功！");
              // 关闭当前页面的标签页;
              store.commit("closeCurrentTag", {
                $router: router,
                $route: route,
              });
            }
            router.push("/admin/building");
          });
        } else {
          return false;
        }
      });
    };
    // 重置
    const onReset = () => {
      formRef.value.resetFields();
    };

    return {
      rules,
      formRef,
      form,
      onSubmit,
      onReset,
    };
  },
};
</script>